package com.zenithmind.chat.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zenithmind.chat.pojo.entity.ChatMessageStatistics;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 消息统计映射器 - 遵循单一职责原则
 * 只负责消息统计的数据访问操作
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Mapper
public interface ChatMessageStatisticsMapper extends BaseMapper<ChatMessageStatistics> {

    /**
     * 根据消息ID查询统计信息
     * 
     * @param messageId 消息ID
     * @return 统计信息
     */
    @Select("SELECT * FROM chat_message_statistics WHERE message_id = #{messageId} AND deleted = 0")
    ChatMessageStatistics selectByMessageId(@Param("messageId") String messageId);

    /**
     * 根据用户ID查询统计信息列表
     * 
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 统计信息列表
     */
    @Select("SELECT * FROM chat_message_statistics WHERE user_id = #{userId} " +
            "AND create_time >= #{startTime} AND create_time <= #{endTime} AND deleted = 0 " +
            "ORDER BY create_time DESC")
    List<ChatMessageStatistics> selectByUserIdAndTimeRange(@Param("userId") String userId,
                                                           @Param("startTime") LocalDateTime startTime,
                                                           @Param("endTime") LocalDateTime endTime);

    /**
     * 根据群组ID查询统计信息列表
     * 
     * @param groupId 群组ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 统计信息列表
     */
    @Select("SELECT * FROM chat_message_statistics WHERE group_id = #{groupId} " +
            "AND create_time >= #{startTime} AND create_time <= #{endTime} AND deleted = 0 " +
            "ORDER BY create_time DESC")
    List<ChatMessageStatistics> selectByGroupIdAndTimeRange(@Param("groupId") String groupId,
                                                            @Param("startTime") LocalDateTime startTime,
                                                            @Param("endTime") LocalDateTime endTime);

    /**
     * 增加点赞数
     * 
     * @param messageId 消息ID
     * @return 更新结果
     */
    @Update("UPDATE chat_message_statistics SET like_count = like_count + 1, update_time = NOW() " +
            "WHERE message_id = #{messageId} AND deleted = 0")
    int incrementLikeCount(@Param("messageId") String messageId);

    /**
     * 减少点赞数
     * 
     * @param messageId 消息ID
     * @return 更新结果
     */
    @Update("UPDATE chat_message_statistics SET like_count = GREATEST(like_count - 1, 0), update_time = NOW() " +
            "WHERE message_id = #{messageId} AND deleted = 0")
    int decrementLikeCount(@Param("messageId") String messageId);

    /**
     * 增加回复数
     * 
     * @param messageId 消息ID
     * @return 更新结果
     */
    @Update("UPDATE chat_message_statistics SET reply_count = reply_count + 1, update_time = NOW() " +
            "WHERE message_id = #{messageId} AND deleted = 0")
    int incrementReplyCount(@Param("messageId") String messageId);

    /**
     * 减少回复数
     * 
     * @param messageId 消息ID
     * @return 更新结果
     */
    @Update("UPDATE chat_message_statistics SET reply_count = GREATEST(reply_count - 1, 0), update_time = NOW() " +
            "WHERE message_id = #{messageId} AND deleted = 0")
    int decrementReplyCount(@Param("messageId") String messageId);

    /**
     * 增加转发数
     * 
     * @param messageId 消息ID
     * @return 更新结果
     */
    @Update("UPDATE chat_message_statistics SET forward_count = forward_count + 1, update_time = NOW() " +
            "WHERE message_id = #{messageId} AND deleted = 0")
    int incrementForwardCount(@Param("messageId") String messageId);

    /**
     * 增加阅读数
     * 
     * @param messageId 消息ID
     * @return 更新结果
     */
    @Update("UPDATE chat_message_statistics SET read_count = read_count + 1, update_time = NOW() " +
            "WHERE message_id = #{messageId} AND deleted = 0")
    int incrementReadCount(@Param("messageId") String messageId);

    /**
     * 统计用户消息总数
     * 
     * @param userId 用户ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 消息总数
     */
    @Select("SELECT COUNT(*) FROM chat_message_statistics WHERE user_id = #{userId} " +
            "AND create_time >= #{startTime} AND create_time <= #{endTime} AND deleted = 0")
    Long countUserMessages(@Param("userId") String userId,
                          @Param("startTime") LocalDateTime startTime,
                          @Param("endTime") LocalDateTime endTime);

    /**
     * 统计群组消息总数
     * 
     * @param groupId 群组ID
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 消息总数
     */
    @Select("SELECT COUNT(*) FROM chat_message_statistics WHERE group_id = #{groupId} " +
            "AND create_time >= #{startTime} AND create_time <= #{endTime} AND deleted = 0")
    Long countGroupMessages(@Param("groupId") String groupId,
                           @Param("startTime") LocalDateTime startTime,
                           @Param("endTime") LocalDateTime endTime);
}
